import 'dart:io';
import 'dart:typed_data';

//import 'package:custom_multi_image_picker/asset.dart';
//import 'package:custom_multi_image_picker/picker.dart';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_native_image/flutter_native_image.dart';
import 'package:multi_image_picker/multi_image_picker.dart';

//import 'package:flutter_image_compress/flutter_image_compress.dart';
import 'package:path_provider/path_provider.dart';
import 'package:uuid/uuid.dart';
import 'package:village_android/component/loading_Dialog.dart';
import 'package:village_android/pages/health/submit/submit_count.dart';
import 'package:village_android/utils/common_utils.dart';

import 'http_utlis.dart';

class ImageUtils {
  //选择图片并上传
  static void pickImageUpLoad(
      BuildContext context, ValueCallback success, ValueCallback error) async {
    //通过MultiImagePicker插件从本地相册选取图片，配置一次最多选择12张，禁止摄像头拍照
    List<Asset> requestList = await MultiImagePicker.pickImages(
      maxImages: 12,
    );

    List files = [];
    List<String> _imagePaths = [];
    if (requestList.length != 0) {
      for (int i = 0; i < requestList.length; i++) {
        Asset asset = requestList[i];

        final String uuid = Uuid().v1();
        //获取图片数据，并转换成uint8List类型
        ByteData byteData = await asset.getByteData();
        List<int> imageData = byteData.buffer.asUint8List();
        print('开始压缩第$i张图片');
//        //通过图片压缩插件进行图片压缩


//        //获得应用临时目录路径
        final Directory _directory = await getTemporaryDirectory();
        final Directory _imageDirectory =
            await new Directory('${_directory.path}/image/')
                .create(recursive: true);
        var _path = _imageDirectory.path;
        print('本次获得路径：${_imageDirectory.path}');
        //将压缩的图片暂时存入应用缓存目录

        File imageFile = new File('${_path}originalImage_$uuid.png')
          ..writeAsBytesSync(imageData);
        _imagePaths.add(imageFile.path);
        print('图片$i的 本地路径是：${imageFile.path}');
        print('开始创建第$i个上传任务');


        File compressedFile = await FlutterNativeImage.compressImage(imageFile.path,
            quality: 70, percentage: 50);
        files.add(new UploadFileInfo(compressedFile, '$uuid.png'));
      }



      FormData formData = new FormData.from({"files": files});
      HttpUtils.postForm("/api/file/uploads",
          formData: formData, context: context, success: (data) {
        if (success != null) {
          success(data, true);
        }
      }, error: (errorMsg) {
        if (success != null) {
          success(errorMsg, true);
        }
      });

      //根据上传任务数量获得上传成功后的图片下载地址

    }
  }
}
